'use strict';

const layout = require('@tuniu/layout');
module.exports = function(config, lightMvc) {
    layout.addPlugin({
        pluginType: 'header',
        styleId: '0',
        viewPath: 'layout/header.ejs',
        viewData: function() {
            let ip = this.req.ip.split(":").slice(-1)[0] || '';
            let cityCode = new Buffer(unescape(this.req.tuniuuser_citycode), "base64").toString();
            return new Promise(function(resolve, reject) {
                let fakeData = {
                    headerData: require(config.srcBaseDir + 'views/layout/header.js').headerData.data
                }
                require('request').get({
                    url: 'http://www.tuniu.com/api/comHeader/getTop/' + JSON.stringify({
                        ip: ip,
                        cityCode: cityCode,
                        from: "tours"
                    })
                }, (err, res, data) => {
                    if (err) {
                        return resolve({
                            headerData: fakeData
                        });
                    }
                    data = new Buffer(data, 'base64').toString();
                    try {
                        data = JSON.parse(data);
                        if (!data.success)
                            return resolve({
                                headerData: fakeData
                            })
                        resolve({
                            headerData: data.data
                        });
                    } catch (e) {
                        resolve({
                            headerData: fakeData
                        });
                    }
                });
            })
        }
    });
    layout.addPlugin({
        pluginType: 'footer',
        styleId: '0',
        viewPath: 'layout/footer.ejs',
        viewData: function() {
            let ip = this.req.ip.split(":").slice(-1)[0] || '';
            let cityCode = new Buffer(unescape(this.req.tuniuuser_citycode), "base64").toString();
            return new Promise(function(resolve, reject) {
                let fakeData = {
                    footerData: require(config.srcBaseDir + 'views/layout/footer.js').footerData.data
                }
                require('request').get({
                    url: `http://www.tuniu.com/api/comFooter/getBottom/${JSON.stringify({
                                ip: ip,
                                cityCode: cityCode,
                                from: "tours"
                            })}`
                }, (err, res, data) => {
                    if (err) {
                        return resolve({
                            footerData: fakeData
                        });
                    }
                    data = new Buffer(data, 'base64').toString();
                    try {
                        data = JSON.parse(data);
                        if (!data.success)
                            return resolve({
                                footerData: fakeData
                            })
                        resolve({
                            footerData: data.data
                        });
                    } catch (e) {
                        resolve({
                            footerData: fakeData
                        });
                    }
                });
            })
        }
    });
    return layout(config.cdn);
}